<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/head::head(~{::title},~{},~{})">
    <title>会员注册-饮食分析系统</title>
</head>

<style>
    /*.login-bg {*/
        /*!*!* background: #eeeeee 0 0 no-repeat; *!*!*/
        /*!*background: url(../images/bg.png) no-repeat center;*!*/
        /*!*background-size: cover;*!*/
        /*!*overflow: hidden;*!*/
        /*background-image: url("/common/images/banner.png");*/
        /*background-size:cover;*/
        /*background-attachment: fixed;*/
        /*background-repeat:no-repeat;*/

    /*}*/
    .login-bg {
        /*background: url(../images/bg.png) no-repeat center;*/
        background-attachment: fixed;
        background-repeat:no-repeat;
        background: url("/common/images/bg2.jpg") no-repeat ;
        background-size: cover;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .weadmin-body {
        background: white;
        padding: 20px;
        width:70%;
        margin: auto;
        /*text-align: center;*/
        /*vertical-align: middle;*/
    }
</style>
<body class="login-bg">
<div class="weadmin-body" style="min-height: 500px">
    <style type="text/css">
        .layui-form-label {
            float: left;
            display: block;
            padding: 9px 15px;
            width: 80px;
            font-weight: 400;
            line-height: 20px;
            text-align: right;
            margin-left: 10%;
        }

    </style>
    <div style="width: 100%;font-weight: bold;font-size: 20px;text-align: center;margin-bottom:20px;">
        <p>请于下方填写个人真实信息</p>
    </div>
    <form class="layui-form layui-form-pane" id="form" enctype="multipart/form-data">
        <input type="hidden" name="type" value="0"/>

        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="we-red">*</span>姓名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="name" name="name" required lay-verify="required"
                       placeholder="姓名" autocomplete="off"  class="layui-input">
            </div>

            <label for="mobile" class="layui-form-label">
                <span class="we-red">*</span>手机号
            </label>
            <div class="layui-input-inline" style="margin-right: 5px">
                <input type="text"  id="mobile" name="mobile" required lay-verify="phone"
                       placeholder="手机号" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="we-red">*</span>将会成为唯一的登录账号
            </div>
        </div>

        <div class="layui-form-item">

            <label for="address" class="layui-form-label">
                <span class="we-red">*</span>联系地址
            </label>
            <div class="layui-input-inline">
                <input type="text"  id="address" name="address"
                       placeholder="联系地址" autocomplete="off" class="layui-input">
            </div>
            <label for="email" class="layui-form-label">
                <span class="we-red">*</span>邮箱
            </label>
            <div class="layui-input-inline">
                <input type="text"  id="email" name="email" lay-verify="email"
                       placeholder="邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_pass" class="layui-form-label">
                密码
            </label>
            <div class="layui-input-inline">
                <input type="password" id="L_pass" name="password" lay-verify="pass"
                       placeholder="登录密码，不填写则不更改" autocomplete="off" class="layui-input">
            </div>
            <label for="L_repass" class="layui-form-label">
                确认密码
            </label>
            <div class="layui-input-inline" style="margin-right: 0px">
                <input type="password" id="L_repass"  lay-verify="repass"
                       placeholder="再次输入新密码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                密码为6到12个字符
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">学生头像</label>
            <div class="layui-input-inline" style="margin-right: 0px">
                <button type="button" onclick="clearImg();" class="layui-btn" id="uploadimg">
                    <i class="layui-icon">&#xe67c;</i>选择图片
                </button>
            </div>
            <div class="layui-input-inline" style="margin-right: 0px" id="imgBox"></div>
        </div>
        <div class="layui-form-item">
            <button style="margin-left: 35%;width: 200px" class="layui-btn" lay-filter="add" lay-submit>注册</button>
        </div>
    </form>
</div>
<script type="text/javascript">
    layui.use(['form', 'layer','upload', 'jquery','laydate', 'admin'], function () {
        var form = layui.form,
            $ = layui.jquery,
            laydate = layui.laydate,
            upload = layui.upload,
            layer = layui.layer;

        laydate.render({
            elem: '#bornDate',
            type: 'datetime'
        });

        //自定义验证规则
        form.verify({
            phone: [/1[34578]\d{9}$/, '请输入正确的手机号'],
            email: [/(.+)@.+\..+$/, '请输入正确的邮箱'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });
        //图片上传
        var uploadInst = upload.render({
            elem: '#uploadimg'
            , multiple: false//多图上传，不支持IE8/9
            , field: 'file'//后台接收字段名
            , auto: false//不自动上传
            , acceptMime: 'image/*'//（只显示图片文件）
            , choose: function (obj) { //选择图片后执行
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                $("#imgBox").empty();
                obj.preview(function (index, file, result) {
                    var img = "<img width='100px' height='100px' src='" + result + "'alt='' class='img-rounded'>";
                    $("#imgBox").append(img);
                });
            }
        });
        //监听提交
        form.on('submit(add)', function (data) {
            console.log(data.field);
            var formData = new FormData($("#form")[0]);
            if (data.field.file == null || data.field.file == "") {
                layer.msg("请上传图片！", {icon: 2, time: 1500});
                return false;
            }
            $.ajax({
                url: "/user/add",
                type: "post",
                data: formData,
                processData: false,
                contentType: false,
                beforeSend: function(){
                    //返回的参数item，即为当前的input DOM对象
                    index = layer.load(1,{shade: [0.3,'grey']});
                },
                success: function (res) {
                    //关闭遮罩层
                    layer.close(index);
                    if (res.success == true) {
                        layer.msg(res.msg, {icon: 1, time: 1500},function () {
                            window.location.href="/admin/login";
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1500});
                    }
                },
                error: function (res) {
                    layer.msg(res, {icon: 2, time: 1500});
                },
                complete: function () {
                }
            });
            return false;
        });
    });
    /*清除回显图片*/
    function clearImg() {
        $("#imgBox").empty();
    }
</script>
</body>

</html>